package com.baosight.sczx.zm.cn.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiColumn;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCP;
import com.baosight.sczx.zm.cn.domain.ZMCNQty;
import com.baosight.sczx.zm.cn.domain.ZMCNQtyMain;
/**
 * 其他样数据
 */
public class ServiceZMCN41 extends ServiceEPBase {
	/**
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化</p> 
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		
		
		return inInfo;
	}
	/**
	 * <p>Function: query</p>
	 * <p>Description: 查询</p> 
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		String fdForeignId=inInfo.getString("fdForeignId");
		if(fdForeignId==null||fdForeignId.trim().equals("")){
			fdForeignId=inInfo.getCellStr("inqu_status", 0, "fdForeignId");
		}
//		String fdReportDepartmentId=inInfo.getString("fdReportDepartmentId");
		inInfo.setCell("inqu_status", 0, "fdForeignId", fdForeignId);
//		inInfo.setCell("inqu_status", 0, "fdReportDepartmentId", fdReportDepartmentId);
		EiInfo outInfo = super.query(inInfo, "ZMCNQty.query", new ZMCNQty());
		
		outInfo.addBlock(getMines()); 
		outInfo.addBlock(StaticBlocks.getStauts4());
		outInfo.addBlock(Department.getInstance().getConstructionUnit("cmes.cp.department", "orgCode", "orgName", false));
		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));//单位的默认值		
		outInfo.set("fdCreateDepartment", UserSession.getInSessionProperty("orgName"));//单位的默认值
		return outInfo;
	}	
	
	public EiBlock getMines() {
		EiBlock block = new EiBlock("cmes.ch.mine");

		EiColumn EiColumn = new EiColumn("display");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		EiColumn = new EiColumn("value");
		EiColumn.setType("C");
		block.addMeta(EiColumn);

		List<Map> _list = new ArrayList<Map>();
		Map<String, String> _map = new HashMap<String, String>();
		_map.put("value", "");
		_map.put("display", "");
		_list.add(_map);

		List<Map> _listData = new ArrayList<Map>();
		try {
			_listData = dao.query("ZMCNQtyMain.queryTreeChild", null);
		} catch (Exception e) {}
		
		for(int i = 0; i < _listData.size(); i++) {
			_list.add(_listData.get(i)); 
		} 
		block.setRows(_list);
		return block;
	}
	
	/**
	 * <p>Function: insert</p>
	 * <p>Description: 新增</p> 
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		EiInfo outinfo = new EiInfo();
		ZMCNQty _dom = new ZMCNQty();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();

		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CN_QTY"));
			this.dao.insert("ZMCNQty.insert", _dom);
		}
		outinfo = this.query(inInfo);
		outinfo.setStatus(1);
		outinfo.setMsg("操作" + rowcount + "记录成功");
		return outinfo;
	}
	 
	/**
	 * <p>Function: update</p>
	 * <p>Description: 修改</p> 
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo outInfo = new EiInfo();
		ZMCNQty _dom = new ZMCNQty();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			boolean validate = this.validate1(inInfo);
			if(validate){
				this.query(inInfo);
				return inInfo;
			}
			this.dao.update("ZMCNQty.update", _dom);
		}
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}	
	/**
	 * <p>Function: delete</p>
	 * <p>Description: 删除</p> 
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo outInfo = new EiInfo();
		ZMCNQty _dom = new ZMCNQty();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			boolean validate = this.validate1(inInfo);
			if(validate){
				this.query(inInfo);
				return inInfo;
			}
			this.dao.delete("ZMCNQty.delete", _dom);
		}
		outInfo = this.query(inInfo);
		outInfo.setStatus(1);
		outInfo.setMsg("操作" + rowcount + "记录成功");
		return outInfo;
	}	
	/**
	 * <p>Function: report</p>
	 * <p>Description: 上报</p> 
	 */
	public EiInfo report(EiInfo inInfo) {
		super.update(inInfo, "ZMCNQty.report");
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 校验是否可以操作
	 * <p>Function: validate</p>
	 * <p>Description: 明细生效后，不允许修改或者删除</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-7-20 09:46:16 李红星 create
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public boolean validate1(EiInfo inInfo) {
		ZMCNQty _dom = new ZMCNQty();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		for (int i = 0; i < rowcount; i++) {
			Map<String, String> params = new HashMap<String, String>();
			_dom.fromMap(eiBlock.getRow(i));
			params.put("fdKeyId", _dom.getFdKeyId());
			List result = dao.query("ZMCNQty.validate1", params);
			if(result != null && result.size() > 0) {
				inInfo.setStatus(-1);
				StringBuffer msg = new StringBuffer("提交失败，");
				msg.append("明细记录已经上报，不能修改或删除！");
				
				inInfo.setMsg(msg.toString());
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 校验是否可以操作
	 * <p>Function: validate</p>
	 * <p>Description: 重复性校验</p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-7-20 09:46:16 李红星 create
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public boolean validate(EiInfo inInfo) {
		ZMCNQty _dom = new ZMCNQty();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		String oper=(String)inInfo.get("efCurButtonEname");
		for (int i = 0; i < rowcount; i++) {
			Map<String, String> params = new HashMap<String, String>();
			_dom.fromMap(eiBlock.getRow(i));
			params.put("fdSjbh", _dom.getFdSjbh());
			if(oper.equalsIgnoreCase("update1")||oper.equalsIgnoreCase("update"))
			{
				params.put("fdKeyId", _dom.getFdKeyId());
			}
			List result = dao.query("ZMCNQty.validate", params);
			if(result != null && result.size() > 0) {
				inInfo.setStatus(-1);
				StringBuffer msg = new StringBuffer("提交失败，");
				msg.append("收据编号重复");
				
				inInfo.setMsg(msg.toString());
				return true;
			}
		}
		return false;
	}
} 